package com.vacomall.erp.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;

import org.apache.velocity.app.event.ReferenceInsertionEventHandler.referenceInsertExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageHelper;
import com.vacomall.erp.dao.CertificateNumMapper;
import com.vacomall.erp.dao.ImagesMapper;
import com.vacomall.erp.model.CertificateNum;
import com.vacomall.erp.model.Images;
import com.vacomall.erp.service.IQuarantineCertificateService;
import com.vacomall.erp.util.ResizeImage;


/**
 * Created by root1 on 16/8/25.
 */
@Service
public class QuarantineCertificateServiceImpl implements IQuarantineCertificateService {

    @Autowired
    private CertificateNumMapper certificateNumMapper;

    @Autowired
    private ImagesMapper imagesMapper;


    /**
     * 检疫证书录入
     * @param certificateNum
     * @return
     */
    public int  add(CertificateNum certificateNum) {
        certificateNum.setMark("0");
        int results = certificateNumMapper.insertSelective(certificateNum);
        //检疫证书录入
        return results;
    }

    /**
     * 更新检疫证书
     * @param certificateNum
     */
    public int edit(CertificateNum certificateNum){
        int results = certificateNumMapper.updateByPrimaryKeySelective(certificateNum);
        return results;
    }

    /**
     * 生成压缩图片保存到数据库
     * @param path  需要压缩图片的路径
     */
    public int addThumbnailImage(String inputFoler,String outputFolder) throws IOException {
        Images images = new Images();
        images.setPictureUrl(inputFoler);
          /*这个参数是要转化成的倍数,如果是1就是转化成1倍*/
        float times = 0.5f;
        File f = new File(inputFoler);
        BufferedImage r1 = ImageIO.read(f);
        String fileFullPath = outputFolder +System.currentTimeMillis()+".jpg";
        ResizeImage.writeHighQuality(ResizeImage.zoomImage(r1,times),fileFullPath);
        images.setThumbnailUrl(fileFullPath);
        imagesMapper.insert(images);
        int results = images.getId();
        return results;
    }


    public Images selectByImage(String number) {
        Images images = imagesMapper.selectByPrimaryKey(Integer.valueOf(number));
        return images;
    }
	
	/**
	 * 根据条件查询检疫证书
	 * @param certificateNum
	 * @return
	 */
	public List<CertificateNum> showCertificate(CertificateNum certificateNum,int pageCurrent,int pageSize ){
		 //分页查询
        PageHelper.startPage(pageCurrent, pageSize);
		List<CertificateNum> list = certificateNumMapper.showByCertificateNum(certificateNum) ;
		return list ;
	}

	/**
	 * 查询检疫证书
	 */
	@Override
	public CertificateNum editPage(Integer id) {
		CertificateNum cNum = certificateNumMapper.selectByPrimaryKey(id);
		return cNum;
	}

	   /**
     * 检疫证书删除
     * @param id
     */
    public int delete(Integer id){
    	int i = certificateNumMapper.deleteByPrimaryKey(id);
    	return i ;
    }

}
